package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"project/models"
	"project/pkg/app"
	"project/pkg/e"
	"project/services"
)

func Get_Students(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchName := ""
	if arg := c.Query("searchName"); arg != "" {
		searchName = arg
	}
	supplierParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchName": searchName,
	}
	err, info, total := services.GetAllStudents(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func Add_Students(c *gin.Context) {
	sno := c.PostForm("sno")
	sname := c.PostForm("sname")
	ssex := c.PostForm("ssex")
	sage := com.StrTo(c.PostForm("sage")).MustInt()
	sdept := c.PostForm("sdept")
	snation := c.PostForm("snation")
	sregion := c.PostForm("sregion")
	sdegree := com.StrTo(c.PostForm("sdegree")).MustInt()
	spolitics_status := c.PostForm("spolitics_status")
	sdisciplinary_record := c.PostForm("sdisciplinary_record")
	smark := c.PostForm("smark")
	err := services.AddStudents(models.Student{sno, sname, ssex, sage, sdept, snation, sregion, sdegree, spolitics_status, sdisciplinary_record, smark})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")

}

func Update_Students(c *gin.Context) {
	sno := c.PostForm("sno")
	sname := c.PostForm("sname")
	ssex := c.PostForm("ssex")
	sage := com.StrTo(c.PostForm("sage")).MustInt()
	sdept := c.PostForm("sdept")
	snation := c.PostForm("snation")
	sregion := c.PostForm("sregion")
	sdegree := com.StrTo(c.PostForm("sdegree")).MustInt()
	spolitics_status := c.PostForm("spolitics_status")
	if sno == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.UpdateStudents(models.Student{sno, sname, ssex, sage, sdept, snation, sregion, sdegree, spolitics_status, "", ""})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func Del_Students(c *gin.Context) {
	code := c.PostForm("sno")
	err := services.DelStudents(code)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}